package com.baidu.controller;

import com.baidu.entity.ShoppingCart;
import com.baidu.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @描述
 * @Author ShoppingCartController
 * @Date 2019/4/24 18:17
 **/
@Controller
@RequestMapping("/shopping")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 插入购物车信息
     * @param sname
     * @param sprice
     * @param stotalprice
     * @param quantity
     * @return
     */
    @RequestMapping("/insertShop")
    @ResponseBody
    public Boolean insertShopping(String sname, BigDecimal sprice, BigDecimal stotalprice, String quantity) {
        System.out.println(sname);
        System.out.println(sprice);
        System.out.println(stotalprice);
        System.out.println(quantity);
        Integer integer = shoppingCartService.insertShopping(sname,quantity,stotalprice,sprice);
        if(integer>0){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 查询购物车数据
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    public List<ShoppingCart> selectShopping() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectShopping();
        List<ShoppingCart> list=new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCarts) {
            ShoppingCart shoppingCart1=new ShoppingCart();
            shoppingCart1.setSname(shoppingCart.getSname());
            shoppingCart1.setSnumber(shoppingCart.getSnumber());
            shoppingCart1.setSprice(shoppingCart.getSprice());
            shoppingCart1.setStotalprice(shoppingCart.getStotalprice());
            shoppingCart1.setQuantity(shoppingCart.getQuantity());
            list.add(shoppingCart1);
        }
        if(list!=null){
            System.out.println(list);
            return list;
        }else{
            return null;
        }
    }
}
